/*
 * @Description:
 * @Autor: fage
 * @Date: 2022-07-28 14:15:58
 * @LastEditors: chenbinfa
 * @LastEditTime: 2022-08-11 15:10:28
 * @description: 描述信息
 * @author: chenbinfa
 */
import {
	DatePicker,
	Input,
	InputNumber,
	Menu,
	Modal,
	Button,
	Tooltip,
	Dropdown,
	Descriptions,
	Select,
	Space,
	Table,
	message,
	Tabs,
	Popconfirm,
	Checkbox,
	Card,
	Form,
	Progress
} from "antd";
import { UserOutlined, DownOutlined, DeleteOutlined, SwapRightOutlined, CopyOutlined } from "@ant-design/icons";
import { NavLink } from "react-router-dom";
import _ from "lodash";
import moment from "moment";
import copy from "copy-to-clipboard";
import AccountIcon from "@/components/AccountIcon";
import {
	formatPowerStr,
	toLocaleString,
	formatterCurrencyStr,
	formatterCurrencyStr2,
	formatterCurrencyStr3,
	formatterSize,
	formatterSizeToTB,
	formatterSizeFromMBToGB,
	formatAddress,
	formatAddressLong
} from "@utils/format";

function formatArr(columns) {
	columns.forEach(t => formatOne(t));
	return columns;
}
function formatOne(column) {
	const t = column;
	if (!t) {
		return;
	}
	if (t.dataIndex && !t.key) {
		t.key = t.dataIndex;
	}
	if (!t.showType) {
		return;
	}
	let tmp = "";
	function makeLink(text, record, index, { isLong, isLink, isCopy }) {
		if (!text) {
			return "";
		}
		// text = tpl(t, text, record);
		let showText = text;
		if (text.length > 20) {
			if (isLong) {
				showText = formatAddressLong(text);
			} else {
				showText = formatAddress(text);
			}
		}
		let link = t.link || t.tpl;
		if (link) {
			for (let k in record) {
				let v1 = record[k];
				let k2 = k + '_s';
				if (typeof v1 == 'object' && k2 in record) {
					v1 = record[k2];
				}
				link = link.replace("{" + k + "}", v1);
			}
		}
		return (
			<Tooltip title={text}>
				{
					isLink && link ? <NavLink to={link} className="link">{showText}</NavLink> : showText
				}
				{isCopy && <span
					onClick={() => {
						copy(text);
						message.success("Copied");
					}}
					className="enable-copy-txt-box">
					&nbsp;
					<CopyOutlined className="copy-icon" />
				</span>}
			</Tooltip>
		);
	}
	switch (t.showType) {
		case "tpl":
			t.render = (text, record, index) => {
				return tpl(t, text, record);
			};
			break;
		case "rank":
			t.render = (text, record, index) => {
				if (!text) text = index + 1;
				if (text > 3) return <div>&nbsp;{text}</div>;
				return <div className="balance-box">
					<span className="money">{text}</span>&nbsp;
					<img className="balance-logo" src={"/img/rank-" + text + ".png"} />
				</div>
			};
			break;
		case "hash":
			t.render = (text, record, index) => {
				return formatAddress(text);
			};
			break;
		case "hashLong":
			t.render = (text, record, index) => {
				return formatAddressLong(text);
			};
			break;
		case "date":
			t.render = (text, record, index) => moment(text).format("YYYY-MM-DD");
			break;
		case "date2":
			t.render = (text, record, index) => moment(text).format("MM-DD");
			break;
		case "time":
			t.render = (text, record, index) => moment(text).format("HH:mm:ss");
			break;
		case "time2":
			t.render = (text, record, index) => moment(text).format("HH:mm");
			break;
		case "datetime":
			if (!t.tpl) {
				t.tpl = "YYYY-MM-DD HH:mm:ss";
			}
			t.render = (text, record, index) => {
				if (t.tpl == "fromNow") {
					let momentO=moment(text);
					tmp = momentO.fromNow();
					if (tmp == "a few seconds ago") {
						tmp = moment().second() - moment(text).second();
						if (tmp < 0) {
							tmp = tmp + 60;
						}
						tmp = tmp + " seconds ago";
					}
					return <Tooltip title={momentO.format('YYYY-MM-DD HH:mm:ss')}>{tmp}</Tooltip>;
				}
				return moment(text).format(t.tpl);
			};
			break;
		case "datetime2":
			t.render = (text, record, index) => moment(text).format("MM-DD HH:mm");
			break;
		case "copy":
			t.render = (text, record, index) => {
				return makeLink(text, record, index, { isLong: false, isLink: false, isCopy: true });
				// if (!text) {
				// 	return "";
				// }
				// text = tpl(t, text, record);
				// let showText = text;
				// if (text.length > 20) {
				// 	showText = formatAddress(text);
				// }
				// return (
				// 	<Tooltip placement="topLeft" title="click to copy">
				// 		<span
				// 			onClick={() => {
				// 				copy(text);
				// 				message.success("Copied");
				// 			}}
				// 			className="enable-copy-txt-box">
				// 			{showText}
				// 			&nbsp;
				// 			<CopyOutlined className="copy-icon" />
				// 		</span>
				// 	</Tooltip>
				// );
			};
			break;
		case "copy-link":
			t.render = (text, record, index) => {
				return makeLink(text, record, index, { isLong: false, isLink: true, isCopy: true });
				// if (!text) {
				// 	return "";
				// }
				// text = tpl(t, text, record);
				// let showText = text;
				// if (text.length > 20) {
				// 	showText = formatAddress(text);
				// }
				// let link = '#';
				// if (t.link) {
				// 	link = t.link;
				// 	for (let k in record) {
				// 		let v1 = record[k];
				// 		let k2 = k + '_s';
				// 		if (typeof v1 == 'object' && k2 in record) {
				// 			v1 = record[k2];
				// 		}
				// 		link = link.replace("{" + k + "}", v1);
				// 	}
				// }
				// return (
				// 	<>
				// 		<NavLink to={link}>{showText}</NavLink>
				// 		<span
				// 			onClick={() => {
				// 				copy(text);
				// 				message.success("Copied");
				// 			}}
				// 			className="enable-copy-txt-box">
				// 			&nbsp;
				// 			<CopyOutlined className="copy-icon" />
				// 		</span>
				// 	</>
				// );
			};
			break;
		case "copyLong":
			t.render = (text, record, index) => {
				return makeLink(text, record, index, { isLong: true, isLink: false, isCopy: true });
				// if (!text) {
				// 	return "";
				// }
				// text = tpl(t, text, record);
				// let showText = text;
				// if (text.length > 20) {
				// 	showText = formatAddressLong(text);
				// }
				// return (
				// 	<Tooltip placement="topLeft" title="click to copy">
				// 		<span
				// 			onClick={() => {
				// 				copy(text);
				// 				message.success("Copied");
				// 			}}
				// 			className="enable-copy-txt-box">
				// 			{showText}
				// 			&nbsp;
				// 			<CopyOutlined className="copy-icon" />
				// 		</span>
				// 	</Tooltip>
				// );
			};
			break;
		case "copyLong-link":
			t.render = (text, record, index) => {
				return makeLink(text, record, index, { isLong: true, isLink: true, isCopy: true });
				// if (!text) {
				// 	return "";
				// }
				// text = tpl(t, text, record);
				// let showText = text;
				// if (text.length > 20) {
				// 	showText = formatAddressLong(text);
				// }
				// let link = '#';
				// if (t.link) {
				// 	link = t.link;
				// 	for (let k in record) {
				// 		let v1 = record[k];
				// 		let k2 = k + '_s';
				// 		if (k == 'from') console.log('==========', k, record)
				// 		if (typeof v1 == 'object' && k2 in record) {
				// 			v1 = record[k2];
				// 		}
				// 		link = link.replace("{" + k + "}", v1);
				// 	}
				// }
				// return (
				// 	<>
				// 		<NavLink to={link}>{showText}</NavLink>
				// 		<span
				// 			onClick={() => {
				// 				copy(text);
				// 				message.success("Copied");
				// 			}}
				// 			className="enable-copy-txt-box">
				// 			&nbsp;
				// 			<CopyOutlined className="copy-icon" />
				// 		</span>
				// 	</>
				// );
			};
			break;
		case "addr":
			t.render = (text, record, index) => {
				return makeLink(text, record, index, { isLong: t.isLong, isLink: true, isCopy: true });
				// if (!text) {
				// 	return "";
				// }
				// text = tpl(t, text, record);
				// let showText = text;
				// if (!t.isSour && text.length > 20) {
				// 	showText = t.isLong ? formatAddressLong(text) : formatAddress(text);
				// }
				// let link = '#';
				// if (t.link) {
				// 	link = t.link;
				// 	for (let k in record) {
				// 		let v1 = record[k];
				// 		let k2 = k + '_s';
				// 		if (typeof v1 == 'object' && k2 in record) {
				// 			v1 = record[k2];
				// 		}
				// 		link = link.replace("{" + k + "}", v1.toString());
				// 	}
				// }
				// return (
				// 	<>
				// 		{t.link ? <NavLink to={link}>{showText}</NavLink> : showText}
				// 		<span
				// 			onClick={() => {
				// 				copy(text);
				// 				message.success("Copied");
				// 			}}
				// 			className="enable-copy-txt-box">
				// 			&nbsp;
				// 			<CopyOutlined className="copy-icon" />
				// 		</span>
				// 	</>
				// );
			};
			break;
		case "accountIcon":
			t.render = (text, record, index) => {
				if (!text) {
					return text;
				}
				let showText = text;
				if (text.length > 20) {
					showText = text.substring(0, 5) + "****" + text.substring(text.length - 5);
				}
				return (
					<Tooltip placement="topLeft">
						<span className="enable-copy-icon-box">
							<AccountIcon
								hash={text}
								onClick={() => {
									copy(text);
									message.success("Copy successful !");
								}}
								title="click copy"
							/>
							<NavLink to={"/account/" + text} title="link" className="link">
								&nbsp;
								{showText}
								&nbsp;
							</NavLink>
							<CopyOutlined
								onClick={() => {
									copy(text);
									message.success("Copy successful !");
								}}
								title="click copy"
							/>
						</span>
					</Tooltip>
				);
			};
			break;
		case "link":
			if (!t.tpl) {
				t.tpl = "./";
			}
			t.render = (text, record, index) => {
				// return makeLink(text, record, index, { isLong: false, isLink: true, isCopy: false });

				let tpl = t.tpl;
				Object.keys(record).forEach(k => {
					let v1 = record[k];
					let k2 = k + '_s';
					if (typeof v1 == 'object' && k2 in record) {
						v1 = record[k2];
					}
					tpl = tpl.replace("{" + k + "}", v1);
				});
				return <NavLink to={tpl} className="link">{text}</NavLink>;
			};
			break;
		case "currency":
			t.render = (text, record, index) => {
				if (!text) {
					return "";
				}
				if (!text.money) {
					console.log(text);
					text = formatterCurrencyStr2(text);
				}
				return (
					<>
						<span className="suffix">CESS</span>
						<span className="money">{text}</span>
					</>
				);
			};
			break;
		case "balance-logo":
			t.render = (text, record, index) => {				
				if (!text) {
					return "--";
				}
				let sour=formatterCurrencyStr3(text);
				// console.log('=========',text,record);
				if (!text.money) {
					text = formatterCurrencyStr2(text);
				}
				return (
					<div className="balance-box">
						<img className="balance-logo" src="/img/cess.png" />
						<span className="money"><Tooltip placement="topLeft" title={sour}>{text}</Tooltip></span>
					</div>
				);
			};
			break;
		case "balance-logo2":
			t.render = (text, record, index) => {				
				if (!text) {
					return "--";
				}
				let sour=formatterCurrencyStr3(text);
				if (!text.money) {
					text = formatterCurrencyStr(text);
				}
				return (
					<Tooltip placement="topLeft" title={sour}><font className="banlance">{text}</font></Tooltip>
				);
			};
			break;
		case "balance-logo3":
			t.render = (text, record, index) => {
				if (!text) {
					return "--";
				}
				let sour=formatterCurrencyStr3(text);
				if (!text.money) {
					text = parseInt(text) / 1e18;
				}
				return (
					<Tooltip placement="topLeft" title={sour}><font className="banlance">{text}</font></Tooltip>
				);
			};
			break;
		case "currency-qianfen":
			t.render = (text, record, index) => {
				if (!text) {
					return "";
				}
				return (
					<>
						<span className="money">{tpl(t, formatterCurrencyStr2(text), record)}</span>
					</>
				);
			};
			break;
		case "qianfen":
			t.render = (text, record, index) => {
				if (!text) {
					return "0";
				}
				if (typeof text == 'string') {
					text = parseFloat(text);
				}
				return text.toLocaleString("zh", { style: "decimal" });
			};
			break;
		case "power":
			t.render = (text, record, index) => {
				return (
					<Tooltip placement="topLeft" title={toLocaleString(text)}>{formatPowerStr(text)}</Tooltip>
				);
			};
			break;
		case "store-size-str":
			t.render = (text, record, index) => {
				if (!text) {
					return "0 B";
				}
				return formatterSize(text);
			};
			break;
		case "store-size-g":
			t.render = (text, record, index) => {
				if (!text) {
					return "";
				}
				return tpl(t, formatterSizeFromMBToGB(text), record);
			};
			break;
		case "store-size-t":
			t.render = (text, record, index) => {
				if (!text) {
					return "";
				}
				return tpl(t, formatterSizeToTB(text), record);
			};
			break;
		case "progress":
			t.render = (text, record, index) => {
				return <Progress style={{ width: 200 }} strokeColor="#0057FF" percent={text} size="small" />;
			};
			break;
	}
	return t;
}
function tpl(column, text, record) {
	if (!column.tpl) return text;
	let tpl = column.tpl;
	Object.keys(record).forEach(k => {
		let v = record[k];
		if (k == column.dataIndex || k == column.key) {
			v = text;
		}
		tpl = tpl.replace("{" + k + "}", v);
	});
	return tpl;
}
function formatDataSource(columns, dataSource) {
	formatArr(columns);
	dataSource.forEach((t, i) => {
		columns.forEach(c => {
			t[c.dataIndex + "_s"] = t[c.dataIndex];
			if (c.render) {
				t[c.dataIndex] = c.render(t[c.dataIndex], t, i);
			} else {
				t[c.dataIndex] = t[c.dataIndex];
			}
		});
	});
}

export { formatArr, formatOne, formatDataSource };
